<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 刮刮卡ajax类
 * @package  	ajax
 * @subpackage  backend
 * @create by allen.qiang
 */

quanli_require('module/base/promotionHandler.class.php');
class ajax_scratch {

    public function execute(){

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //查询微信互动活动
                    $this->searchScratch();
                    break;
                case 2: //保存微信互动活动
                    $this->saveScratch();
                    break;
                case 3: //编辑微信互动活动
                    $this->editScratch();
                    break;
                case 4: //禁用使用
                    $this->useAtion();
                    break;
                case 5: //删除微信互动活动
                    $this->deleteScratch();
                    break;
                case 6: //查询微信抽奖记录
                    $this->searchWeixinLog();
                    break;
                case 7: //查询微信中奖记录
                    $this->searchWinLog();
                    break;
                case 8: //查询微信中奖记录
                    $this->getOneScratch();
                    break;
                default:
                    $this->toReturn('参数错误');
                    break;

            }

        } else {

            $this->toReturn('参数错误');

        }

    }

    /**
     * 返回信息
     * @param string $message
     * @param int $code
     * @param mixed $data
     */
    private function toReturn( $message, $code = 400, $data = '' ) {

        die( json_encode( array( 'code' => $code, 'message' => $message, 'data' => $data ) ) );

    }

    //查询微信互动活动
    private function searchScratch(){
        global $G_PGLOB;

        $arg_array = $_REQUEST;

        $promotionHandler = new promotionHandler($G_PGLOB->DBCA);

        $result_data = $promotionHandler->searchWxActivity($arg_array);

        $result_array = array(
            'page'  => $arg_array['page'] ,
            'total' => $arg_array['count'] ,
            'rows'  => array()
        );

  
        if ($result_data) {

            $edit_permission = $G_PGLOB->has_permission(4521);

            $time = date('Y-m-d H:i:s');

            foreach($result_data as $val) {

                if($edit_permission){

                    //判断是否在活动期间内
                    $starttotime = $val['startDate'].' 00:00:00';

                    $endtotime = $val['endDate'].' 23:59:59';

                    if($time >= $starttotime && $time <= $endtotime){

                        $result_array['rows'][] = array(
                            'id' 	=> $val['promotionID'],
                            'cell' 	=> array(
                                 '<a  href="javascript: void(0);" onclick="scratch.drawRecord('.$val['promotionID'].')">抽奖记录</a>&nbsp;
                              <a  href="javascript: void(0);" onclick="scratch.winRecord('.$val['promotionID'].')">中奖记录</a>&nbsp;
                              <a style="display:none"  href="javascript: void(0);" onclick="scratch.countAnalysis('.$val['promotionID'].')">统计分析</a>
                              ',
                                $val['promotionName'],
                                $val['promotionTypeName'],
                                $val['startDate'],
                                $val['endDate'],
                                $val['participateTime'],
                                $val['isPointsExchange']?'是':'否',
                                $val['points'],
                                $val['maxExchangeTime'],
                                $val['acceptEndDate'],
                                $val['useFlg'] == 1 ?
                                    '<a href="javascript: void(0);" onclick=scratch.doAction(\'' . $val['promotionID'] . '\',0);>'.$val['useFlgName'].'</a>':
                                    '<a href="javascript: void(0);" onclick=scratch.doAction(\'' . $val['promotionID'] . '\',1); >禁用</a>',
                            ),
                        );

                    }else{

                        $result_array['rows'][] = array(
                            'id' 	=> $val['promotionID'],
                            'cell' 	=> array(
                                ' <a href="javascript: void(0);" onclick="scratch.openHTML.edit('.$val['promotionID'].')">修改</a>&nbsp;
                              <a  href="javascript: void(0);" onclick="scratch.drawRecord('.$val['promotionID'].')">抽奖记录</a>&nbsp;
                              <a  href="javascript: void(0);" onclick="scratch.winRecord('.$val['promotionID'].')">中奖记录</a>&nbsp;
                              <a style="display:none"  href="javascript: void(0);" onclick="scratch.countAnalysis('.$val['promotionID'].')">统计分析</a>
                              ',
                            $val['promotionName'],
                            $val['promotionTypeName'],
                            $val['startDate'],
                            $val['endDate'],
                            $val['totalTime'],
                            $val['participateTime'],
                            $val['isPointsExchange']?'是':'否',
                            $val['points'],
                            $val['maxExchangeTime'],
                            $val['acceptEndDate'],
                            $val['useFlg'] == 1 ?
                                '<a href="javascript: void(0);" onclick=scratch.doAction(\'' . $val['promotionID'] . '\',0);>'.$val['useFlgName'].'</a>':
                                '<a href="javascript: void(0);" onclick=scratch.doAction(\'' . $val['promotionID'] . '\',1); >禁用</a>',
                        ),
                        );
                    }

                }else{

                    $result_array['rows'][] = array(
                        'id' 	=> $val['promotionID'],
                        'cell' 	=> array(
                            $val['promotionName'],
                            $val['promotionTypeName'],
                            $val['startDate'],
                            $val['endDate'],
                            $val['participateTime'],
                            $val['isPointsExchange']?'是':'否',
                            $val['points'],
                            $val['maxExchangeTime'],
                            $val['acceptEndDate'],
                            $val['useFlg'] == 1 ?
                                '启用':
                                '禁用',
                        ),
                    );

                }

            }
        }

        echo json_encode( $result_array );

    }
    //保存微信互动活动
    private function saveScratch(){
        global $G_PGLOB;

        $arg_array = $_REQUEST;

        //获取随机数
        foreach($arg_array['datacache'] as &$val){

            $rateArr = explode('/',$val[1]);

            $val['awardsRatio'] = $rateArr[0];

            $val['awardsRate'] = $rateArr[1];

            $val['randNum'] = $this->getRand($rateArr[0],$rateArr[1]);

        }

        $arg_array['creator'] = $G_PGLOB->currentUserID;

        $promotionHandler = new promotionHandler($G_PGLOB->DBCA);

        $result = $promotionHandler->saveWxActivity($arg_array);

        echo json_encode($result);

    }
    //编辑微信互动活动
    private function editScratch(){

        global $G_PGLOB;

        $arg_array = $_REQUEST;

        //判断当前是否在活动时间内
        $time = date('Y-m-d H:i:s');

        $promotionHandler = new promotionHandler($G_PGLOB->DBCA);

        $promotion = $promotionHandler->searchWxActivity($arg_array);

        $starttotime = $promotion[0]['startDate'].' 00:00:00';

        $endtotime = $promotion[0]['endDate'].' 23:59:59';

        if($time>=$starttotime && $time<=$endtotime){
            echo json_encode(array('code'=>400,'message'=>'当前正在活动期间内，禁止修改该活动'));
            return;
        }


        //获取随机数
        foreach($arg_array['datacache'] as &$val){

            $rateArr = explode('/',$val[1]);

            $val['awardsRatio'] = $rateArr[0];

            $val['awardsRate'] = $rateArr[1];

            $val['randNum'] = $this->getRand($rateArr[0],$rateArr[1]);

        }

        $arg_array['creator'] = $G_PGLOB->currentUserID;

        $promotionHandler = new promotionHandler($G_PGLOB->DBCA);

        $result = $promotionHandler->editWxActivity($arg_array);

        echo json_encode($result);

    }
    //禁用使用
    private function useAtion(){
        global $G_PGLOB;

        $arg_array = $_REQUEST;

        $promotionHandler = new promotionHandler($G_PGLOB->DBCA);

        $result = $promotionHandler->actionUseWx($arg_array);

        echo json_encode($result);
    }
    //删除微信互动活动
    private function deleteScratch(){

        global $G_PGLOB;

        $arg_array = $_REQUEST;

        $promotionHandler = new promotionHandler($G_PGLOB->DBCA);

        $result = $promotionHandler->deleteWxActivity($arg_array);

        echo json_encode($result);

    }

    //获取不相同的随机数
    private function getRand($num,$awardsRate){

        $num_arr = array();//存放随机数数组

        for($i=0;$i<$num;$i++){

            switch($awardsRate){
                case 100:
                    $randNum = mt_rand(10000,10100);
                    break;
                case 1000:
                    $randNum = mt_rand(10000,11000);
                    break;
                case 10000:
                    $randNum = mt_rand(10000,20000);
                    break;
            }

            if(in_array($randNum,$num_arr)){
                $i--;
                continue;
            }

            $num_arr[] = $randNum;

        }

        return $num_arr;

    }

    //查询微信抽奖记录
    private function searchWeixinLog(){

        global $G_PGLOB;

        //查询
        $promotionHandler = new promotionHandler( $G_PGLOB->DBCA ) ;

        $arg_array = $_REQUEST;

        $result = $promotionHandler->searchWxLogInfo( $arg_array ) ;

        //查询结果
        $result_array = array(
            'page'  => $arg_array['page'],
            'total' => $arg_array['count'],
            'rows'  => array()
        );

        if ($result) {
            foreach($result as $key => &$data) {

                $result_array['rows'][] = array(
                    'id' 	=> $key,
                    'cell' 	=> array(
                        $data['date'],
                        $data['cust_count'],
                        $data['log_count'],
                        $data['prize_count']
                    )
                );
            }

        }

        echo json_encode( $result_array );

    }

    //查询中奖纪录
    private function searchWinLog(){

        global $G_PGLOB;


        $promotionHandler = new promotionHandler( $G_PGLOB->DBCA ) ;

        $arg_array = $_REQUEST;
        //查询
        $result = $promotionHandler->searchWinLogInfo( $arg_array ) ;

        //查询结果
        $result_array = array(
            'page'  => $arg_array['page'],
            'total' => $arg_array['count'],
            'rows'  => array()
        );

        if ($result) {
            foreach($result as $data) {

                $result_array['rows'][] = array(
                    'id' 	=> $data['promotionID'],
                    'cell' 	=> array(
                        $data['pcustID'],
                        $data['awardsName'],
                        $data['prize'],
                        $data['isReceiveName']
                    )
                );
            }

        }

        echo json_encode( $result_array );

    }

    private function getOneScratch(){

        global $G_PGLOB;

        $promotionHandler = new promotionHandler($G_PGLOB->DBCA);

        $arg_array = $_REQUEST;

        $promotion = $promotionHandler->searchWxActivity($arg_array);

        $promotion_detail = $promotionHandler->searchWxActivityDetail($arg_array);

        $promotion[0]['promotion_detail'] = $promotion_detail;

        echo json_encode($promotion[0]);

    }
}
